/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-06     HZHJYYJQR       the first version
 */
#ifndef APPLICATIONS_HJCOMM_HJRFCARD_H_
#define APPLICATIONS_HJCOMM_HJRFCARD_H_

#include <fcntl.h>

#define RFCard_Head         0xaabb


#define FindCardCmd         0x0102   //寻卡
#define AntiCollisionCmd    0x0202   //防冲突
#define SelectCardCmd       0x0302   //选卡
#define attestateCmd        0x0702   //认证


#define ReadCardCmd         0x0802   //一键读卡
#define WriteCardCmd        0x0902   //一键写卡

#define InitValueCmd         0x0A02     //初始化值
#define ReadValueCmd         0x0B02     //读值
#define ReChargeCmd          0x0D02     //充值
#define DecreaseValueCmd     0x0C02     //减值


#define CardPassword        0xffffffffffff    //卡片密码

class RFCard {

public:
    uint8_t     head1;
    uint8_t     head2;
    uint16_t    len;
    uint16_t    device;
    uint16_t    cmd;
    uint8_t     status;
    uint8_t     data[1024];
    uint16_t    data_len;
    uint8_t     sum;

    //组包 包->数组
    void make_pack(uint8_t* buff, uint16_t* len);

    //解包 数组->包
    void unmake_pack(uint8_t* buff, uint16_t* len);

    uint8_t xor_sum(uint8_t* buff, uint16_t len);
};

class RFCardRecv {

public:
    bool proc_one_byte(RFCard* rfcard, uint8_t data);

private:
    uint8_t recv_buff[1024 + 20] = {0};
    uint16_t recv_len = 0;
    uint16_t recv_index = 0;
};

#endif /* APPLICATIONS_HJCOMM_HJRFCARD_H_ */
